package Input;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import Announcements.Announcement;
import Constructions.*;
import Main.SpaceGame;
import Menus.BuildMenu;
import Power.*;
import World.TileType;


public class BuildMenuInput implements KeyListener 
{
	BuildMenu menu;
	SpaceGame main;
	public BuildMenuInput(SpaceGame main,BuildMenu m) 
	{
		this.main = main;
		this.menu = m;
	}

	public void keyPressed(KeyEvent e) 
	{
		if (e.getKeyCode() == KeyEvent.VK_D)
		{
			menu.setType(null);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_L)
		{
			menu.setType(ConstructionType.LifeSupport);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_M)
		{
			menu.setType(ConstructionType.AsteroidMiner);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_H)
		{
			menu.setType(ConstructionType.Door);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_T)
		{
			menu.setType(ConstructionType.TeleportPad);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_P)
		{
			menu.setType(ConstructionType.PowerConnector);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_S)
		{
			menu.setType(ConstructionType.SolarPanel);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_W)
		{
			menu.setType(ConstructionType.TorpedoBay);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_R)
		{
			menu.setType(ConstructionType.Recycler);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_I)
		{
			menu.setType(ConstructionType.Morgue);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_Q)
		{
			menu.setType(ConstructionType.PPS);
		}
		
		else if (e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			boolean canPlace = true;
			if(menu.getType() != null)
			{
				if (main.resources.getMetal() >= menu.getType().getMetalCost())
				{
					if (main.resources.getBiomass() >= menu.getType().getBioCost())
					{
						for(int i = 0;i <= menu.getType().getWidth()-1;i++)
						{
							for (int j = 0;j<= menu.getType().getHeight()-1;j++)
							{
								if (main.world.tiles[main.cursor.getX()+i][main.cursor.getY()+j].hasConstruction() || main.world.tiles[main.cursor.getX()+i][main.cursor.getY()+j].getType() != menu.getType().getRequiredTile())
									canPlace = false;
							}
						}
						if (canPlace)
						{
							main.resources.setMetal(main.resources.getMetal()-menu.getType().getMetalCost());
							main.resources.setBiomass(main.resources.getBiomass()-menu.getType().getBioCost());
							
							if (menu.getType() == ConstructionType.LifeSupport)
								main.constructionManager.add(new LifeSupport(main.cursor.getX(),main.cursor.getY(),main));
							
							else if (menu.getType() == ConstructionType.AsteroidMiner)
								main.constructionManager.add(new AsteroidMiner(main.cursor.getX(), main.cursor.getY(), main));
							
							else if (menu.getType() == ConstructionType.Door)
							{
								main.constructionManager.add(new Door(main.cursor.getX(), main.cursor.getY(), main));
							}
							
							else if (menu.getType() == ConstructionType.TeleportPad)
								main.constructionManager.add(new TeleportPad(main.cursor.getX(), main.cursor.getY(), main));
							
							else if (menu.getType() == ConstructionType.PowerConnector)
								main.powerManager.add(new PowerConnector(main.cursor.getX(), main.cursor.getY(), main));
							
							else if (menu.getType() == ConstructionType.TorpedoBay)
								main.constructionManager.add(new TorpedoBay(main.cursor.getX(), main.cursor.getY(), main));
							
							else if (menu.getType() == ConstructionType.PPS)
								main.constructionManager.add(new PPS(main.cursor.getX(), main.cursor.getY(), main));
							
							else if (menu.getType() == ConstructionType.Recycler)
								main.constructionManager.add(new Recycler(main.cursor.getX(), main.cursor.getY(), main));
							
							else if (menu.getType() == ConstructionType.Morgue)
								main.constructionManager.add(new Morgue(main.cursor.getX(), main.cursor.getY(), main));
							
							else if (menu.getType() == ConstructionType.SolarPanel)
							{
								boolean hasWall = false;
								for(int x = -1;x<=1;x+=2)
								{
									for(int y = -1;y<=1;y+=2)
									{
										if(main.world.tiles[main.cursor.getX()+x][main.cursor.getY()+y].getType() == TileType.Hull)
											hasWall = true;
										for(Generator g:main.powerManager.generatorList)
										{
											if (g.getX() == main.cursor.getX()+x || g.getY() == main.cursor.getY()+y)
												hasWall = true;
										}
									}
								}
								if(hasWall && main.world.tiles[main.cursor.getX()][main.cursor.getY()].getType() == TileType.Space)
									main.powerManager.addGenerator(new SolarPanel(main.cursor.getX(), main.cursor.getY(), main));
								else
									main.announcementManager.AddAnnouncement(new Announcement("You need to place solar panels against a wall or panel and in space.",Color.red));
							}
						}
						else
						{
							main.announcementManager.AddAnnouncement(new Announcement("You cant place this there!", Color.RED));
						}
					}
					else
					{
						main.announcementManager.AddAnnouncement(new Announcement("Not Enough Biomatter!  You need "+menu.getType().getBioCost()+" biomatter to get that.", Color.red));
					}
				}
				else
				{
					main.announcementManager.AddAnnouncement(new Announcement("Not Enough Metal!  You need "+menu.getType().getMetalCost()+" metal to get that.", Color.red));
				}
			}
			else
			{
				main.constructionManager.remove(main.cursor.getX(),main.cursor.getY());
				main.powerManager.remove(main.cursor.getX(), main.cursor.getY());
				main.powerManager.removeGenerator(main.cursor.getX(), main.cursor.getY());
			}
		}
		
		if(menu.getType() != null)
			main.cursor.setCursorSize(menu.getType().getWidth(), menu.getType().getHeight());
		else
			main.cursor.setCursorSize(1, 1);
	}

	public void keyReleased(KeyEvent arg0) 
	{
		
	}

	public void keyTyped(KeyEvent arg0) 
	{
		
	}
	
}
